import 'package:amap_all_fluttify/amap_all_fluttify.dart';
import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_base_info_model.dart';
import 'package:fzcgx_app/ui/widget/bottom_sheet_widget.dart';

class FactoryBaseInfoViewModel extends BaseViewModel {
  FactoryBaseInfoModel model;
  TextEditingController comNameController = TextEditingController();
  TextEditingController legalNameController = TextEditingController();
  TextEditingController legalMobileController = TextEditingController();
  TextEditingController comTelController = TextEditingController();
  TextEditingController comAddressController = TextEditingController();
  TextEditingController detailController = TextEditingController();

  FactoryBaseInfoViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.post<FactoryBaseInfoModel>(
        'app/call/getusers_baseinfo_gc', null, onSuccess: (data) {
      if (data != null) {
        model = data;
        comNameController.text = model.comName;
        legalNameController.text = model.legalName;
        legalMobileController.text = model.legalMobile;
        comTelController.text = model.comTel;
        comAddressController.text = model.comAddress;
        detailController.text = model.detail;
        this.setState(ViewState.Success);
      } else {
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void onPostApi(BuildContext context, Function callback) {
    if (comNameController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的工作单位');

      return;
    }
    if (legalNameController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入法人姓名');

      return;
    }
    if (legalMobileController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的手机号');

      return;
    }
    if (comTelController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的座机号');

      return;
    }
    if (model.comCity.isEmpty) {
      FLToast.showText(text: '请选择您的工厂位置');

      return;
    }
    if (comAddressController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入具体门牌号');

      return;
    }
    if (detailController.text.toString().isEmpty) {
      FLToast.showText(text: '请输入您的工厂简介');

      return;
    }
    var dismiss = FLToast.loading(text: '正在保存...');
    Http.instance.post('app/call/setusers_baseinfo_gc', {
      'comCity': model.comCity,
      'com_address': comAddressController.text,
      'com_name': comNameController.text,
      'com_tel': comTelController.text,
      'legal_mobile': legalMobileController.text,
      'legal_name': legalNameController.text,
      'detail': detailController.text,
      'legal_sex': model.legalSex,
      "lng": model.lng,
      "lat": model.lat,
    }, onSuccess: (data) {
      FLToast.showSuccess(text: '保存成功');
      dismiss();
      callback();
      Navigator.pop(context);
    }, onError: (error) {
      dismiss();
    }, onFail: (fail) {
      dismiss();
    });
  }

  void onShowBottomSheet(BuildContext context) {
    showDialog(
        barrierDismissible: true, //是否点击空白区域关闭对话框,默认为true，可以关闭
        context: context,
        builder: (BuildContext context) {
          var list = List();
          list.add('男');
          list.add('女');

          return BottomSheetWidget(
            list: list,
            onItemClickListener: (index) async {
              print(list[index]);
              model.legalSex = list[index].toString() == '男' ? '1' : '2';
              notifyListeners();
              Navigator.pop(context);
            },
          );
        });
  }

  void updatePoiInfo(PoiInfo poiInfo) {
    model.lat = poiInfo.poi.latLng.latitude.toString();
    model.lng = poiInfo.poi.latLng.longitude.toString();
     model.comCity=poiInfo.poi.cityName+poiInfo.poi.adName+poiInfo.poi.address;
    notifyListeners();
  }
}
